package com.foreknow.demo14;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {
    private PreparedStatement pstmt;
    private  ResultSet rs;
    private  Connection conn;
    /**
     * 连接数据库
     * @param
     */
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        //1. 加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接Connection
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_pro","root","123456");
        return conn;
    }

    /**
     *
     * @param sql
     * @param values 动态可变参数 Object[]
     * @return
     * @throws SQLException
     */
    public ResultSet query(String sql,Object...values) throws SQLException { //select *from users where username=? and password=?
        //3. 获取PreparedStatement对象用于执行动态的SQL语句
        pstmt = conn.prepareStatement(sql);
        for(int i = 0;i<values.length;i++){ //将？规则为具体的值(数组中的元素)
            pstmt.setObject(i+1,values[i]);
        }
        rs = pstmt.executeQuery();
        return rs;
    }

    /**
     * DML
     * @param
     */
    public int update(String sql,Object...values) throws SQLException {
        //3. 获取PreparedStatement对象用于执行动态的SQL语句
        pstmt = conn.prepareStatement(sql);
        for(int i = 0;i<values.length;i++){ //将？规则为具体的值(数组中的元素)
            pstmt.setObject(i+1,values[i]);
        }
        int isRight = pstmt.executeUpdate();
        return isRight;
    }

    /**
     * 关闭资源
     * @param
     */
    public void closed() throws SQLException {
        if(rs != null) {
            rs.close();
        }
        if(pstmt != null) {
            pstmt.close();
        }
        if(conn != null) {
            conn.close();
        }
    }
    public static void main(String[] args) {
        DBUtil dbUtil = new DBUtil();
        try {
            Connection connection = dbUtil.getConnection(); //连接数据库
            ResultSet rs = dbUtil.query("select * from users where name=? and email=?","tom","tom@163.com");
            while (rs.next()) {
                int userId = rs.getInt("user_id");
                String name = rs.getString("name");
                System.out.println(userId+"---"+name);
            }
            //int isRight = dbUtil.update("delete from users where user_id=?",4);
            //            if(isRight == 1) {
            //                System.out.println("删除成功......");
            //            }else {
            //                System.out.println("删除失败......");
            //            }
            dbUtil.closed();//关闭资源
            //            ResultSet rs = dbUtil.query("select * from users");//调用查询的方法
            //            List<Users> list = new ArrayList<>();
            //            while (rs.next()) { //将指针向下移动
            //                Users users = new Users();
            //                //将当前行数据保存到对象中
            //                users.setUserId(rs.getInt("user_id"));
            //                users.setName(rs.getString("name"));
            //                users.setEmail(rs.getString("email"));
            //                //将当前对象保存到集合中
            //                list.add(users);
            //            }
            //            System.out.println(list.size());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }











}
